package org.xiaobai.ai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import org.xiaobai.ai.entity.ChatPaperDetailVO;
import org.xiaobai.ai.entity.ChatPaperVO;
import org.xiaobai.ai.request.ChatPaperCreateRequest;
import org.xiaobai.ai.request.ChatPaperGenerateRequest;
import org.xiaobai.ai.request.ChatPaperQueryRequest;
import org.xiaobai.ai.service.ChatPaperDetailService;
import org.xiaobai.ai.service.ChatPaperService;
import org.xiaobai.common.utils.UserUtil;
import org.xiaobai.tool.response.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author dingfeng.xiao
 * @description ai max
 * @date 2023/8/16 10:12
 */
@Api(value = "ai 增强工具", tags = {"ai 增强工具"})
@CrossOrigin
@RestController
@RequestMapping("/chat/user/pro")
@Validated
public class AIProController {
    @Resource
    private ChatPaperService chatPaperService;
    @Resource
    private ChatPaperDetailService chatPaperDetailService;

    /**
     * 论文删除
     */
    @ApiOperation(value = "论文删除")
    @PostMapping("/paper/delete")
    public Result<Boolean> paperDelete(@NotNull(message = "ID不能为空") Long paperId) throws IOException {
        return Result.success(chatPaperService.removeById(paperId));
    }
    /**
     * 论文导出
     */
    @ApiOperation(value = "论文导出")
    @PostMapping("/paper/my")
    public Result<Page<ChatPaperVO>> paperMy(@Valid @RequestBody ChatPaperQueryRequest queryRequest) throws IOException {
        return Result.success(chatPaperService.paperMy(queryRequest));
    }

    /**
     * 创建论文信息
     */
    @ApiOperation(value = "创建论文信息")
    @PostMapping("/paper/create")
    public Result<Long> paperCreate(@Valid @RequestBody ChatPaperCreateRequest createRequest) {
        createRequest.setUserId(UserUtil.getUserId());
        return Result.success(chatPaperService.paperCreate(createRequest));
    }

    /**
     * 开始论文生成大纲
     */
    @ApiOperation(value = "开始论文生成大纲")
    @PostMapping("/paper/start")
    public ResponseBodyEmitter paperStart(@NotNull(message = "ID不能为空") Long paperId) {
        return chatPaperService.paperStart(paperId);
    }

    /**
     * 开始论文生成大纲
     */
    @ApiOperation(value = "开始论文生成大纲")
    @PostMapping("/paper/generate")
    public ResponseBodyEmitter paperGenerate(@Valid @RequestBody ChatPaperGenerateRequest generateRequest) {
        return chatPaperService.paperGenerate(generateRequest);
    }

    /**
     * 论文导出
     */
    @ApiOperation(value = "论文导出")
    @PostMapping("/paper/export")
    public Result<String> paperExport(@NotNull(message = "ID不能为空") Long paperId, String exportType) throws IOException {
        return Result.success(chatPaperService.paperExport(paperId, exportType));
    }

    /**
     * 论文导出
     */
    @ApiOperation(value = "论文明细")
    @PostMapping("/paper/detail")
    public Result<List<ChatPaperDetailVO>> paperDetail(@NotNull(message = "ID不能为空") Long paperId) {
        return Result.success(chatPaperDetailService.findByPaperId(paperId));
    }
}
